﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.ModelBinding;
using System.Web.Http.OData;
using System.Web.Http.OData.Routing;
using IIS_Local4.Models;

namespace IIS_Local4.Controllers
{
    /*
    이 컨트롤러에 대한 경로를 추가하려면 WebApiConfig 클래스를 추가로 변경해야 할 수 있습니다. 가능한 경우 WebApiConfig 클래스의 Register 메서드에 이 문을 병합합니다. OData URL은 대/소문자를 구분합니다.

    using System.Web.Http.OData.Builder;
    using System.Web.Http.OData.Extensions;
    using IIS_Local4.Models;
    ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
    
    config.Routes.MapODataServiceRoute("odata", "odata", builder.GetEdmModel());
    */
    public class containersController : ODataController
    {
        private Model1 db = new Model1();

        // GET: odata/containers
        [EnableQuery]
        public IQueryable<container> Getcontainers()
        {
            return db.containers;
        }

        // GET: odata/containers(5)
        [EnableQuery]
        public SingleResult<container> Getcontainer([FromODataUri] string key)
        {
            return SingleResult.Create(db.containers.Where(container => container.container_name == key));
        }

        // PUT: odata/containers(5)
        public async Task<IHttpActionResult> Put([FromODataUri] string key, Delta<container> patch)
        {
            Validate(patch.GetEntity());

            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            container container = await db.containers.FindAsync(key);
            if (container == null)
            {
                return NotFound();
            }

            patch.Put(container);

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!containerExists(key))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return Updated(container);
        }

        // POST: odata/containers
        public async Task<IHttpActionResult> Post(container container)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.containers.Add(container);

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                if (containerExists(container.container_name))
                {
                    return Conflict();
                }
                else
                {
                    throw;
                }
            }

            return Created(container);
        }

        // PATCH: odata/containers(5)
        [AcceptVerbs("PATCH", "MERGE")]
        public async Task<IHttpActionResult> Patch([FromODataUri] string key, Delta<container> patch)
        {
            Validate(patch.GetEntity());

            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            container container = await db.containers.FindAsync(key);
            if (container == null)
            {
                return NotFound();
            }

            patch.Patch(container);

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!containerExists(key))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return Updated(container);
        }

        // DELETE: odata/containers(5)
        public async Task<IHttpActionResult> Delete([FromODataUri] string key)
        {
            container container = await db.containers.FindAsync(key);
            if (container == null)
            {
                return NotFound();
            }

            db.containers.Remove(container);
            await db.SaveChangesAsync();

            return StatusCode(HttpStatusCode.NoContent);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }

        private bool containerExists(string key)
        {
            return db.containers.Count(e => e.container_name == key) > 0;
        }
    }
}
